﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KeDa2025_
{
    public partial class user : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["User"] != null)
            {
                InitStudent();
            }
            else
            {
                Response.Write("<script>alert('请先登录!');window.location.href='denglu.aspx'</script>");
            }

        }
        private void InitStudent()
        {
            //创建数据库链接
            MySqlConnection connection = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;");
            MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter("select * from users", connection);

            DataSet ds = new DataSet();
            //填充容器
            sqlDataAdapter.Fill(ds);
            // 检查数据是否加载成功         

            //把容器给repeater
            rpUser.DataSource = ds;
            rpUser.DataBind();
        }

        protected void rpUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Delete")
            {
                string sql = string.Format("delete from users where id={0}", id);
                MySqlConnection connection = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;");
                MySqlCommand cmd = new MySqlCommand(sql, connection);
                connection.Open();
                int flag = cmd.ExecuteNonQuery();
                connection.Close();
                if (flag > 0)
                {
                    Response.Write("<script>alert('删除成功');</script>");
                    InitStudent();

                }
            }
            else
            {
                Response.Redirect("register.aspx?id=" + id);
            }

        }

        
          

        protected void Button2_Click(object sender, EventArgs e)
        {
            #region 全选删除
            int sign = 0;
            //遍历所有的数据行
            for (int i = 0; i < rpUser.Items.Count; i++)
            {
                //获取每一行的勾选框
                CheckBox cbx = rpUser.Items[i].FindControl("cbxItem") as CheckBox;
                int noteId = Convert.ToInt32((rpUser.Items[i].FindControl("hfNoteId") as HiddenField).Value);
                //判断勾选框是否勾选
                if (cbx.Checked == true)
                {

                    string sql = string.Format("delete from users where id=" + noteId);
                    MySqlConnection connection = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;");
                    MySqlCommand cmd = new MySqlCommand(sql, connection);
                    connection.Open();
                    int flag = cmd.ExecuteNonQuery();
                    connection.Close();

                    if (flag > 0)
                    {
                        sign++;
                    }
                }
            }
            if (sign > 0)
                Response.Write($"<script>alert('删除成功，共删除{sign}条')</script>");
            InitStudent();

            #endregion
        }
    }

}
 